package com.atguigu.core.controller;


import cn.hutool.core.util.IdUtil;
import com.atguigu.core.bean.R;
import com.atguigu.core.repository.TPayRepository;
import com.atguigu.entity.TPay;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 交易支付表 前端控制器
 * </p>
 *
 * @author kashim
 * @since 2024-04-16
 */
@Tag(name = "支付接口 （断路器版）")
@RestController
@RequestMapping("/pay-circuit")
public class PayCircuitBreakerController {
    @Autowired
    private TPayRepository tPayRepository;

    @Operation(summary = "查询支付")
    @GetMapping("/circuit/{id}")
    public R getPay(@PathVariable("id") Integer id) {

        if (id == -4) throw new RuntimeException("-------Circuit ID 不能为负数");


        if (id == 9999) {
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
//            throw new RuntimeException(e);
            }
        }

        return R.ok().data(tPayRepository.getById(id));
    }

    @Operation(summary = "查询支付")
    @GetMapping("/bulkhead/{id}")
    public R bulkhead(@PathVariable("id") Integer id) {

        if (id == -4) throw new RuntimeException("-------Circuit ID 不能为负数");


        if (id == 9999) {
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
//            throw new RuntimeException(e);
            }
        }

        return R.ok().data("hello,bulkhead! input id:" + id + "\t" + IdUtil.simpleUUID());
    }
    @Operation(summary = "查询支付")
    @GetMapping("/ratelimit/{id}")
    public R ratelimit(@PathVariable("id") Integer id) {

        if (id == -4) throw new RuntimeException("-------Circuit ID 不能为负数");


        if (id == 9999) {
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
//            throw new RuntimeException(e);
            }
        }

        return R.ok().data("hello,bulkhead! input id:" + id + "\t" + IdUtil.simpleUUID());
    }


    @Value("${server.port}")
    private String port;

    @GetMapping("/get/info")
    public String getInfoByConsul(@Value("${atguigu.info}") String atguiguInfo) {
        return "atguiguInfo:" + atguiguInfo + "\t " + "port:" + port;

    }


}

